#ifndef ANALYSER_H
#define ANALYSER_H

#include "mad.h"
#include "fmod.h"
#include <QtCore/QFile>
#include <QtCore/QString>
#include <QtCore/QThread>
#include <vector>
using namespace std;

class Analyser : public QThread
{
	Q_OBJECT

public :
	Analyser(QString);
	~Analyser();
	QString filePath(void);
	void setCanceled(void);

protected :
	void run(void);

	struct DecodeInfo {
	    unsigned char const  *start;
		unsigned int length;
		Analyser *parent;
	};

	void decode(unsigned char const *, unsigned int);
	enum mad_flow perframe(struct mad_header const *, struct mad_pcm *);
	static enum mad_flow input(void *, struct mad_stream *);
    static enum mad_flow output(void *, struct mad_header const *, struct mad_pcm *);
    static enum mad_flow error(void *, struct mad_stream *, struct mad_frame *);

private :
	void preAnalyse(void);
	void depAnalyse(void);
	void handlePcmData(void);

signals :
	void readyToAnalyse(unsigned int);
	void analysing(unsigned int);
	void analysed(unsigned int *, QString, bool);
	void tagsGot(QString, QString, QString);
	//void debuginfo(QString); //debug

private :
	FMOD_SYSTEM   *system;
	FMOD_SOUND    *sound;
	QString title;
	QString artist;
	QString album;

	QString      filepath;
	unsigned int filesize;
	unsigned int lensec;
	unsigned int frames;
	unsigned int count;
	bool canceled;

	char *inputbuffer;
    mad_timer_t *sec;
    mad_stream *stream;
	mad_header *header;
    mad_frame *frame;
	mad_decoder *decoder;

	int *pcmdata;
	DecodeInfo *data;
	vector<unsigned int> framePoint;
	unsigned int pos[14];
};

#endif
